﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyClass {
	public class Topic {
		public string topicName;
		public string description;
		public int status; //0: pending, 1: running, 2: finished
		public int lower, upper, maxTeams;
		public List<Team> teams;

		public Topic(string t, string d, int l, int u, int m) {
			topicName = t; description = d;
			lower = l; upper = u; maxTeams = m;
			teams = new List<Team>();
			status = 0;
		}

		public string Export() {
			string s = "Topic Name: " + topicName + "\r\n";
			s += "  Descriptioni: " + description + "\r\n";
			int cnt = 1;
			foreach (Team team in teams) {
				s += "  Team" + cnt++.ToString() + ". Name: " + team.teamName + "  Captain: " + team.captainID + "\r\n";
				s += "    Members:";
				foreach (string mID in team.members) s += " " + mID;
				s += "\r\n";
			}
			return s;
		}

		public int ResidualCapacity() {
			return (maxTeams - teams.Count) * upper;
		}

		public void DeleteButton_Click(object sender, EventArgs e) {
			TeamButton button = (TeamButton)sender;
			teams.Remove(button.team);
			Controller.GetController.UpdateTopicPanel(this);
		}

		public void FreshPage_Click(object sender, EventArgs e) {
			Controller.GetController.UpdateTopicPanel(this);
		}

		public Panel GetPanel() {
			FlowLayoutPanel p0 = new FlowLayoutPanel();
			p0.FlowDirection = FlowDirection.TopDown;
			p0.Width = 375;
			Label l = new Label();
			l.Width = 375;
			l.AutoSize = true;
			l.Text = "Topic Name: " + topicName + "\nLower: " + lower.ToString() + ", Upper: " + upper.ToString() + ", " + "Max teams: " + maxTeams;
			l.Text += "\nDescription: " + description;
			Button fresh = new Button();
			fresh.AutoSize = true;
			fresh.Click += new EventHandler(FreshPage_Click);
			fresh.Text = "Fresh the Page";
			p0.Controls.Add(l);
			p0.Controls.Add(fresh);

			MyConsole.WriteLine("Topic " + topicName + "'s GetPanel is called, # of teams = " + teams.Count.ToString());
			FlowLayoutPanel panel = new FlowLayoutPanel();
			panel.AutoSize = true;
			panel.FlowDirection = FlowDirection.TopDown;
			panel.Controls.Add(p0);
			int cnt = 1;
			foreach (Team t in teams) {
				panel.Controls.Add(t.GetPanel(cnt++));
			}
			return panel;
		}

		public Dictionary<string, string> ToDict() {
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict["TopicName"] = topicName;
			dict["Status"] = status.ToString();
			dict["Lower"] = lower.ToString();
			dict["Upper"] = upper.ToString();
			dict["MaxTeams"] = maxTeams.ToString();
			dict["NTeams"] = teams.Count.ToString();
			int cnt = 0;
			foreach (Team t in teams) {
				dict["Team" + cnt++.ToString()] = t.ToJSON();
			}
			return dict;
		}
	}
}
